home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Libris Britannia 4
/
science library(b).zip
/
science library(b)
/
DDJMAG
/
DDJ8801.ZIP
/
NARO.ZIP
/
READCFG.C
< prev
next >
Wrap
Text File
|
1987-10-30
|
6KB
|
245 lines
/*
Copyright (C) 1987 Paradigm Systems Inc. All rights reserved.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "loc.h"
#include "externs.h"
int process_class_keyword(), process_order_keyword() ;
int process_rom_keyword(), process_dup_keyword() ;
static struct CFG_COMMANDS {
char *cmd ;
int (*command)() ;
} cfg_cmds[] = {
"CLASS", process_class_keyword,
"ORDER", process_order_keyword,
"ROM", process_rom_keyword,
"DUP", process_dup_keyword,
} ;
static unsigned int line ;
int process_locate_file(seg_list)
SEG_DESCRIPTOR *seg_list ;
{
int i, error = OK;
char *tok, *buf ;
/*
This function reads the configuration file and performs the parsing
and control transfer to routines which perform the desired action.
*/
/* Allocate some memory for the line buffer */
if ((buf = malloc(256)) == NULL) {
perror(__FILE__) ;
exit(1) ;
}
/* Read and categorize a token from the configuration file */
line = 0 ;
while (fgets(buf, 256, config_file) != NULL) {
line++ ;
/* Extract the first token (read the next line if none is found */
if ((tok = strtok(buf, " \t\n")) == NULL)
continue ;
if (*tok == ';')
continue ;
for (i = 0; i < dim(cfg_cmds); i++) {
if (stricmp(cfg_cmds[i].cmd, tok) == 0) {
error = (*cfg_cmds[i].command)() ;
break ;
}
}
if (i == dim(cfg_cmds)) {
fprintf(stderr, "Illegal input on line %d\n", line) ;
exit(1) ;
}
}
free(buf) ;
return error ;
}
int process_class_keyword()
{
char *tok, name[32], *p ;
unsigned int seg ;
/*
This function parses the remainder of the CLASS directive.
*/
/* Read the class name */
if ((tok = strtok(NULL, " \t\n")) == NULL) {
fprintf(stderr, "Line %d: Class name expected\n", line) ;
return ERROR ;
}
strcpy(name, strupr(tok)) ;
/* Attempt to read the next token */
if ((tok = strtok(NULL, " \t\n")) == NULL) {
fprintf(stderr, "Line %d: \"=\" expected\n", line) ;
return ERROR ;
}
/* Verify that an equal sign is present */
if (strcmp(tok, "=") != 0) {
fprintf(stderr, "Line %d: \"=\" expected\n", line) ;
return ERROR ;
}
/* Read the segment number for the class */
if ((tok = strtok(NULL, " \t\n")) == NULL) {
fprintf(stderr, "Line %d: Segment number expected\n", line) ;
return ERROR ;
}
seg = (unsigned int) strtol(tok, &p, 0) ;
if (*p != '\0') {
fprintf(stderr, "Line %d: Segment number expected\n", line) ;
return ERROR ;
}
/* Assign the physical segment number to the specified class */
if (assign_physical_segment(name, seg) == ERROR) {
fprintf(stderr, "Line %d: Undefined class <%s>\n", line, name) ;
return ERROR;
}
return OK ;
}
int process_order_keyword()
{
char *tok, pclass[32], class[32] ;
unsigned int next_seg, count = 0 ;
/*
This function processes the ORDER directive.
*/
/* Read the leading class name from the command */
if ((tok = strtok(NULL, " \t\n")) == NULL) {
fprintf(stderr, "Line %d: Class name expected\n", line) ;
return ERROR ;
}
strcpy(pclass, strupr(tok)) ;
/* Process the remaining class names in the command */
while ((tok = strtok(NULL, " \t\n")) != NULL) {
if (*tok == ';')
break ;
strcpy(class, strupr(tok)) ;
count++ ;
/* Compute the segment address for this class to be made contiguous
with the previous class */
if (get_next_segment(pclass, class, &next_seg) == ERROR) {
fprintf(stderr, "Line %d: Undefined class\n", line) ;
return ERROR ;
}
/* Assign the computed segment number to the class */
if (assign_physical_segment(class, next_seg) == ERROR) {
fprintf(stderr, "Line %d: Undefined class <%s>\n", line, class) ;
return ERROR ;
}
/* Setup to process the next class */
strcpy(pclass, class) ;
}
if (count == 0) {
fprintf(stderr, "Line %d: Missing class\n", line) ;
return ERROR ;
}
return OK ;
}
int process_dup_keyword()
{
char *tok, old_class[32], new_class[32] ;
/*
This function is responsible for processing the DUP directive.
*/
/* Read the existing class name */
if ((tok = strtok(NULL, " \t\n")) == NULL) {
fprintf(stderr, "Line %d: Missing class\n", line) ;
return ERROR ;
}
strcpy(old_class, strupr(tok)) ;
/* Read the name of the class to be created */
if ((tok = strtok(NULL, " \t\n")) == NULL) {
fprintf(stderr, "Line %d: Missing class\n", line) ;
return ERROR ;
}
if (*tok == ';') {
fprintf(stderr, "Line %d: Missing class\n", line) ;
return ERROR ;
}
strcpy(new_class, strupr(tok)) ;
/* Duplicate the existing class */
if (dup_class(old_class, new_class) == ERROR) {
fprintf(stderr, "Line %d: Undefined class <%s>\n", line, old_class) ;
return ERROR ;
}
return OK ;
}
int process_rom_keyword()
{
unsigned int count = 0 ;
char *tok, class[32] ;
/*
This function processes the ROM keyword and marks all specified
classes as ROMable.
*/
/* Read all of the tokens on the line */
while ((tok = strtok(NULL, " \t\n")) != NULL) {
if (*tok == ';')
break ;
strcpy(class, strupr(tok)) ;
if (rom_class(class) == ERROR) {
fprintf(stderr, "Line %d: Undefined class <%s>\n", line, class) ;
return ERROR ;
}
count++ ;
}
/* Flag the case of no classes specified following the directive */
if (count == 0) {
fprintf(stderr, "Line %d: Missing class\n", line) ;
return ERROR ;
}
return OK ;
}